package com.lwx.sys.controller;


import com.lwx.sys.domain.Notice;
import com.lwx.sys.domain.User;
import com.lwx.sys.service.NoticeService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 公告模块
 * @author lwx
 * @since 2020-04-08
 */
@Controller
@RequestMapping("/notice")
class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 获取所有的公告信息
     * @param noticeVo
     * @return
     */
    @GetMapping("loadAllNotice")
    @ResponseBody
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        return noticeService.loadAllNotice(noticeVo);
    }

    /**
     * 删除公告信息
     * @param id id
     * @return
     */
    @DeleteMapping("/deleteNotice")
    @ResponseBody
    public ResultObj deleteNotice(Integer id){
        try {
            noticeService.deleteNotice(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除公告信息
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDeleteNotice")
    @ResponseBody
    public ResultObj batchDeleteNotice(Integer[] ids){
        try {
            noticeService.batchDeleteNotice(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加公告信息
     * @param notice 公告信息
     * @param session session
     * @return
     */
    @PostMapping("/addNotice")
    @ResponseBody
    public ResultObj addNotice(Notice notice, HttpSession session){
        try {
            //从session中获取登录的用户名
            String userName = (String) session.getAttribute("userName");
            //发布人
            notice.setOpername(userName);
            notice.setCreatetime(new Date());
            noticeService.addNotice(notice);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新公告信息
     * @param notice
     * @return
     */
    @PostMapping("/updateNotice")
    @ResponseBody
    public ResultObj updateNotice(Notice notice){
        try {
            noticeService.updateNotice(notice);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id查询公告信息
     * @param id
     * @return
     */
    @GetMapping("loadNoticeById")
    @ResponseBody
    public DataGridView loadNoticeById(Integer id){
        return new DataGridView(noticeService.getById(id));
    }
}

